package xiaobange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xiaobange.common.BaseConText;
import xiaobange.common.R;
import xiaobange.entity.ShoppingCart;
import xiaobange.service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class shoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> stringR(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        lambdaQueryWrapper.eq(ShoppingCart::getUserId , BaseConText.getThreadSessionID());
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }


    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取用户ID
        Long threadSessionID = BaseConText.getThreadSessionID();
        shoppingCart.setUserId(threadSessionID);
        //判断用户加入购物车的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId , threadSessionID);
        if (shoppingCart.getDishId() != null){//判断是否是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId , shoppingCart.getDishId());
        }else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId , shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(lambdaQueryWrapper);
        //判断购物车中是否存在该菜品/套餐
        if (cart != null){
            //存在则给+1
            cart.setNumber(cart.getNumber() + 1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(cart);
        }else {
            //不存在则添加到购物车
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }


    /**
     * 减去购物车的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> Sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断一下是菜品还是套餐
        if (shoppingCart.getDishId() != null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId , shoppingCart.getDishId());
        }else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId , shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
        //判断数量是否为1，如果为1则删除
        if (one.getNumber() > 1){
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        }else {
            shoppingCartService.removeById(one);
        }

        return R.success("one");
    }


    @DeleteMapping("clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId , BaseConText.getThreadSessionID());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("succeed");
    }



}
